﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Docking2010.Views;
using DevExpress.XtraEditors;
using Sfcorp.form;
using TT.Module.sysLogin;
using DevExpress.XtraSplashScreen;
using SfcorpDLL.biz;
using SfcorpDLL.ety;
using TT.ORM;
using Bill.BasForm.sfcorpBill;
using TT.Module.frmUsers;
using System.Collections;
using Dev.Control;
using System.IO;

namespace Sfcorp
{
    public partial class MainForm : XtraForm
    {
        public MainForm()
        {
            InitializeComponent();
            tabbedView1.DocumentAdded += tabbedView_DocumentAdded;
            tabbedView1.DocumentRemoved += tabbedView_DocumentRemoved;
            //tabbedView1.FloatDocuments.CollectionChanged += tabbedView_FloatDocumentsCollectionChanged;
        }

        void tabbedView_DocumentAdded(object sender, DocumentEventArgs e)
        {
            RegisterDocument(e.Document);
        }

        void tabbedView_DocumentRemoved(object sender, DocumentEventArgs e)
        {
            UnregisterDocument(e.Document);
        }

        void RegisterDocument(BaseDocument document)
        {
            Documents.Add(document.Form.Tag, document);
        }

        void UnregisterDocument(BaseDocument document)
        {
            if (document.Form != null)
                Documents.Remove(document.Form.Tag);
        }

        IDictionary<object, BaseDocument> documentsCore = new Dictionary<object, BaseDocument>();

        public IDictionary<object, BaseDocument> Documents
        {
            get { return documentsCore; }
        }

        private void navBarControl1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            try
            {
                try
                {
                    splashScreenManager1.ShowWaitForm();
                }
                catch { }
                BaseDocument document = null;
                string frmName = e.Link.Item.Tag.ToString();

                #region 特殊界面调用
                switch (frmName)
                {
                    case "ChrgPass":
                        etySendParam eyChrgPass = new etySendParam();
                        eyChrgPass.SQLHELP = APP.SqlHelp;
                        eyChrgPass.PORT_CODE = APP.PORT_CODE;
                        eyChrgPass.LST_USER = APP.User_ID;
                        eyChrgPass.LST_MOD = "ChrgPass";
                        frmChangePass fmChrgPass = new frmChangePass();
                        fmChrgPass.Tag = eyChrgPass;
                        try { splashScreenManager1.CloseWaitForm(); }  catch { }
                        fmChrgPass.ShowDialog();
                        break;
                    case "bnsSeach":
                        if (!Documents.TryGetValue(frmName, out document))
                        {
                            frmSeachDtl fmMySelf = new frmSeachDtl();
                            fmMySelf.MdiParent = this;
                            fmMySelf.Show();
                            try{ splashScreenManager1.CloseWaitForm();  } catch { }
                            return;
                        }
                        else tabbedView1.Controller.Activate(document);
                        break;
                    case "bnsDaySeach":
                        if (!Documents.TryGetValue(frmName, out document))
                        {
                            frmSearchSummary fmMySelf = new frmSearchSummary();
                            fmMySelf.MdiParent = this;
                            fmMySelf.Show();
                            try{ splashScreenManager1.CloseWaitForm();  } catch { }
                            return;
                        }
                        else tabbedView1.Controller.Activate(document);
                        break;
                    case "Refresh":
                        RefreshBaseTables();
                        try{ splashScreenManager1.CloseWaitForm();  } catch { }
                        TT.HelpInfo.MsgBox.msgBox("刷新成功。",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        return;
                        break;
                    case "CreateSec":       //权限扫描
                        if (TT.HelpInfo.MsgBox.ShowQuestionBox("是否确定建立权限？\r\n建议系统管理员执行。") == false) return;
                        global.bzUser.CreateMainSecrity(bar.Items, global.basSysPara.UserID);
                        try{ splashScreenManager1.CloseWaitForm();  } catch { }
                        TT.HelpInfo.MsgBox.msgBox("权限建立成功。",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        return;
                        break;
                    case "MySelf":          //个性设定
                        if (!Documents.TryGetValue(frmName, out document))
                        {
                            frmMyParam fmMySelf = new frmMyParam();
                            fmMySelf.MdiParent = this;
                            fmMySelf.Show();
                            try{ splashScreenManager1.CloseWaitForm();  } catch { }
                            return;
                        }
                        else tabbedView1.Controller.Activate(document);
                        break;
                    case "MainTag":
                        if (!Documents.TryGetValue(frmName, out document))
                        {
                            MainTag mainTag = new MainTag();
                            mainTag.MdiParent = this;
                            mainTag.Show();
                            try{ splashScreenManager1.CloseWaitForm();  } catch { }
                            return;
                        }
                        else tabbedView1.Controller.Activate(document);
                        break;
                }
                #endregion 

                #region 普通界面调用
                if (!Documents.TryGetValue(frmName, out document))
                {
                    BaseFormDev frmOpen;
                    switch (frmName)
                    {
                        case "AccountBilling":
                            frmOpen = new frmBillingMonth();
                            break;
                        case "ClientSet":
                            frmOpen = new frmClient();
                            break;
                        case "CustomerSet":
                            frmOpen = new frmCustomer();
                            break;
                        case "Customer-Client":
                            frmOpen = new frmCustomerClient();
                            break;
                        case "AccountCZ":
                            frmOpen = new frmAccountCZ();
                            break;
                        case "AccountDMT":
                            frmOpen = new frmAccountDMT();
                            break;
                        case "CntrCorpSet":
                            frmOpen = new frmCntrCorp();
                            break;
                        case "Bns":
                            frmOpen = new frmAgyBns();
                            break;
                        case "BillCode":
                            frmOpen = new frmBillCode(global.basSysPara);
                            break;
                        case "BillProject":
                            frmOpen = new frmBillProject(global.basSysPara);
                            break;
                        case "BillPrice":
                            frmOpen = new frmBillPrice(global.basSysPara);
                            break;
                        case "Account":
                            frmOpen = new frmAccount();
                            break;
                        //case "AccountPre":
                            //frmOpen = new frmAccountPre();
                            //break;
                        case "User":
                            frmOpen = new frmUserSec(global.basSysPara);
                            break;
                        default:
                            return;
                    }
                    frmOpen.MOD_ID = e.Link.Item.Name;
                    frmOpen.MdiParent = this;
                    frmOpen.Show();
                }
                else 
                    tabbedView1.Controller.Activate(document);
                try { splashScreenManager1.CloseWaitForm(); } catch { }
                
                #endregion 

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        } 
        private void RefreshBaseTables()
        {
            global.ilFeeWay = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_FEE_WAY");
            global.ilCustomerEnabled = global.bzSfcorp.GetComboboxSource("CustEnabled", "");
            global.ilCollaboRate = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_COLLABORATE");
            global.ilCIE = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_I_E");
            global.ilFeeMod = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_FEE_MOD");
            global.ilPort = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_PORT");
            global.ilWorkWay = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_WORK_WAY");
            global.ilCntrSize = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_CNTR_SIZ");
            global.ilCntrType = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_CNTR_TYP");

            global.ilClient = global.bzSfcorp.GetBaseTableSource<etyClient>("CBO_C_CLIENT");
            global.ilCustomer = global.bzSfcorp.GetBaseTableSource<SfcorpDLL.ety.etyCustomer>("CBO_C_CUSTOMER");
            global.ilCntrCorp = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_CNTR_CORP");
            global.ilCntrCtrl = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_CNTR_CTRL");
            global.ilCntrOper = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_CNTR_OPER");
            global.ilCargo = global.bzSfcorp.GetBaseTableSource<etyCodeRef>("CBO_C_CARGO");
            global.ilBillCode = global.bzSfcorp.GetBaseTableSource<TT.ORM.etyCodeRef>("CBO_C_BILL_CODE");
            global.ilBillCode_I = global.bzSfcorp.GetBaseTableSource<TT.ORM.etyCodeRef>("CBO_C_BILL_CODE_I");
            global.ilBillIO = global.bzSfcorp.GetBaseTableSource<TT.ORM.etyCodeRef>("CBO_C_BILL_IO");
            global.ilBillUnit = global.bzSfcorp.GetBaseTableSource<TT.ORM.etyCodeRef>("CBO_C_BILL_UNIT");
            global.ilBnsSpcMod = global.bzSfcorp.GetBaseTableSource<TT.ORM.etyCodeRef>("CBO_C_BNS_SPC_MOD");
            global.ilForeignVvd = global.bzSfcorp.GetBaseTableSource<TT.ORM.etyCodeRef>("CBO_C_FOREIGN_VVD");
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.splashScreenManager1 = new SplashScreenManager(this, typeof(global::Sfcorp.form.frmPleaseWaitMe), true, true);
            //初始化
            global.basSysPara.SqlHelp = APP.SqlHelp;
            global.basSysPara.PORT_CODE = APP.PORT_CODE;
            global.basSysPara.UserID = APP.User_ID;
            global.basSysPara.SqliBaseHelp = APP.SQLiteBaseTable;
            global.basSysPara.ProgramPath = APP.ProgramPath;
            global.bzSfcorp = new bizSfcorp(global.basSysPara,bar.Items);       //权限设置
            global.bzUser = new bizUsers(global.basSysPara.SqlHelp, global.basSysPara.PORT_CODE);
            TT.HelpInfo.MsgBox.TITLE = "润港货代系统消息提示";

            RefreshBaseTables();
            MainTag("MainTag");

            try
            {
                SplashScreenManager.CloseForm();
            }
            catch { }

            //图片加载
            //图标
            //string MainPicture = System.Windows.Forms.Application.StartupPath + @"\image\MainPicture.jpg";
            //if (TT.Common.FileProcess.fileExist(MainPicture))
            //{
            //    //Bitmap bmtemp = new Bitmap(MainPicture);
            //    //this.picture.Image = bmtemp;

            //    Bitmap bmPic = new Bitmap(MainPicture);
            //    Point ptLoction = new Point(bmPic.Size);
            //    if (ptLoction.X > picture.Size.Width || ptLoction.Y > picture.Size.Height)
            //    {
            //        //图像框的停靠方式   
            //        //pcbPic.Dock = DockStyle.Fill;   
            //        //图像充滿图像框，並且图像維持比例   
            //        //picture.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.StretchHorizontal;
            //        picture.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
            //    }
            //    else
            //    {
            //        //图像在图像框置中   
            //        picture.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Clip;
            //    }

            //    //LoadAsync：非同步转入图像   
            //    picture.LoadAsync(MainPicture);
            //}

            
        }

        private void MainTag(string iLinkName)
        {
            splashScreenManager1.ShowWaitForm();
            BaseDocument document = null;
            string frmName = iLinkName;
            if (!Documents.TryGetValue(frmName, out document))
            {
                MainTag mainTag = new MainTag();
                mainTag.MdiParent = this;
                mainTag.Show();
                try{ splashScreenManager1.CloseWaitForm();  } catch { }
                return;
            }
            else tabbedView1.Controller.Activate(document);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            this.Close();
            Application.Exit();
        }

    }
}
